﻿using System;

// 版权所有：2014  All Rights Reserved.
// 文 件 名：SysHelper.cs
// 功能描述：
// 创建标识：m.sh.lin0328@163.com 2014/12/6 14:10:55
// 修改描述：
//----------------------------------------------------------------*/
namespace MslDiary.Framework.Tool
{
    public class SysHelper
    {
        private Microsoft.Win32.RegistryKey rKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);

        #region 自动启动

        /// <summary>
        /// 读取注册表
        /// </summary>
        /// <returns></returns>
        public string Read(string name)
        {
            if (rKey.GetValue(name) == null)
            {
                return String.Empty;
            }
            return rKey.GetValue(name).ToString();
        }
        /// <summary>
        /// 是否开机启动
        /// </summary>
        public bool IsAutoStartup()
        {
            string app = Read(MslDiary.Framework.Gloable.ApplicationName).Trim();
            if (app.Length > 0 && app.EndsWith(".exe\""))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 取消自动启动
        /// </summary>
        /// <param name="setAutoRun">设置/取消自动启动</param>
        public void AutoStartup(bool setAutoRun)
        {
            AutoStartup(setAutoRun, Environment.CommandLine);
        }

        /// <summary>
        /// 系统设置/取消自动启动
        /// </summary>
        /// <param name="setAutoRun">是否自动启动</param>
        /// <param name="appPath">系统执行路径（可增加配置参数）</param>
        public void AutoStartup(bool setAutoRun, string appPath)
        {
            try
            {
                if (setAutoRun)
                {
                    rKey.SetValue(MslDiary.Framework.Gloable.ApplicationName, appPath);
                }
                else
                {
                    rKey.DeleteValue(MslDiary.Framework.Gloable.ApplicationName, false);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion
    }
}
